﻿using KidChoreApp.Data.IDataAdapters.Adapters;
using KidChoreApp.Data.IDataAdapters.Interfaces;
using KidChoreApp.DataModels;
using KidChoreApp.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;


namespace KidChoreApp.Web.Controllers
{
    [Authorize]
    public class ChoreController : Controller
    {
        IKidChoreAdapter _adapter = new KidChoreAdapter();
        
        [AllowAnonymous]
        public ActionResult ChoreIndex()
        {
            List<Chore> chores = _adapter.GetAllChores();
            return View(chores);
        }
        
        [HttpGet]
        public ActionResult AddChore()
        {
            if (User.IsInRole("Admin"))
            {
                return View();
            }
            return RedirectToAction("ChoreIndex");
        }
        [HttpPost]
        public ActionResult AddChore(string name, string room, bool isdone, bool isrecurring)
        {
            if (User.IsInRole("Admin"))
            {
                Chore newChore = new Chore();
                newChore.Name = name;
                newChore.Room = room;
                newChore.IsDone = isdone;
                newChore.IsRecurring = isrecurring;
                newChore = _adapter.AddChore(newChore);
                return RedirectToAction("ChoreDetail/" + newChore.Id);
            }
            return RedirectToAction("ChoreIndex");
        }
        public ActionResult ChoreDetail(int id)
        {
            ChoreDetailVM vm = new ChoreDetailVM();
            vm.Chore = _adapter.GetChore(id);
            vm.Kids = _adapter.GetKidsForChore(vm.Chore.Id);
            return View(vm);
        }
        [HttpPost]
        public ActionResult RemoveKidFromChore(int kidId, int choreId)
        {
            if (User.IsInRole("Admin"))
            {
                _adapter.DeleteKidChore(kidId, choreId);
                return RedirectToAction("ChoreDetail/" + choreId);
            }
            return RedirectToAction("ChoreDetail/" + choreId);
        }

        [HttpGet]
        public ActionResult AddKidToChore(int id)
        {
            if (User.IsInRole("Admin"))
            {
                AddKidToChoreVM vm = new AddKidToChoreVM();
                vm.Chore = _adapter.GetChore(id);
                vm.KidsAssigned = _adapter.GetKidsForChore(vm.Chore.Id);
                vm.KidsUnassigned = _adapter.GetAllKids();
                for (int i = 0; i < vm.KidsAssigned.Count(); i++)
                {
                    for (int j = 0; j < vm.KidsUnassigned.Count(); j++)
                    {
                        if (vm.KidsAssigned[i].Id == vm.KidsUnassigned[j].Id)
                        {
                            vm.KidsUnassigned.Remove(vm.KidsUnassigned[j]);
                            break;
                        }

                    }

                }

                return View(vm);
            }
            return RedirectToAction("ChoreDetail/" + id);
        }
        [HttpPost]
        public ActionResult AddKidToChore(int kidId, int choreId)
        {
            if (User.IsInRole("Admin"))
            {
                Kid kid = _adapter.GetKid(kidId);
                Chore chore = _adapter.GetChore(choreId);
                _adapter.AddKidChore(kid, chore);

                return RedirectToAction("AddKidToChore/" + choreId);
            }
            return RedirectToAction("ChoreDetail/" + choreId);
        }
        public ActionResult DeleteChore(int id)
        {
            if (User.IsInRole("Admin"))
            {
                _adapter.DeleteChore(id);
                return RedirectToAction("ChoreIndex");
            }
            return RedirectToAction("ChoreDetail/" + id);
        }
        [HttpGet]
        public ActionResult EditChore(int id)
        {
            if (User.IsInRole("Admin"))
            {
                return View(_adapter.GetChore(id));
            }
            return RedirectToAction("ChoreDetail/" + id);
        }
        [HttpPost]
        public ActionResult EditChore(Chore chore)
        {
            if (User.IsInRole("Admin"))
            {
                chore = _adapter.UpdateChore(chore);
            }
            return RedirectToAction("ChoreDetail/" + chore.Id);
        }
	}
}